﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static LightControllerDll.RsClassLibControllerDll;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrackBar;

namespace CLVisionLightControlTS
{
    internal class RSLight : AbstractLightDevice
    {
        /// <summary>
        /// 初始化，连接光源
        /// </summary>
        /// <param name="IP"></param>
        /// <param name="iPort"></param>
        /// <param name="DataSocket"></param>
        /// <param name="ErrorCode"></param>
        /// <param name="ErrorText"></param>
        /// <param name="Occurred"></param>
        /// <returns></returns>
        public override bool Init(string IP, int iPort, out uint DataSocket, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "success";
            Occurred = false;
            DataSocket = RseeController_ConnectNet(IP, iPort);
            if (DataSocket == 0)
            {
                ErrorCode = -200;
                ErrorText = "光源网口连接失败";
                Occurred = true;
                return false;
            }
            // false:脱机模式  true:在线模式
            int state = RseeController_P_S2_24V_10TCL_TriEn(DataSocket, false);
            if (state != 0)
            {
                StateOut(state, out ErrorCode, out ErrorText, out Occurred);
                ErrorText = "OfflineOrOnline" + ErrorText;
                Occurred = true;
                return false;
            }
            return true;
        }

        /// <summary>
        /// 释放，断开光源
        /// </summary>
        /// <param name="DataSocket"></param>
        /// <param name="ErrorCode"></param>
        /// <param name="ErrorText"></param>
        /// <param name="Occurred"></param>
        /// <returns></returns>
        public override bool DeInit(uint DataSocket, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "success";
            Occurred = false;
            if (!RseeController_CloseNet(DataSocket))
            {
                ErrorCode = -201;
                ErrorText = "网口断开失败";
                Occurred = true;
                return false; ;
            }
            return true;
        }

        /// <summary>
        /// 设置触发模式
        /// </summary>
        /// <param name="DataSocket"></param>
        /// <param name="Mode"></param>
        /// <param name="ErrorCode"></param>
        /// <param name="ErrorText"></param>
        /// <param name="Occurred"></param>
        /// <returns></returns>
        public override bool TriggerMode(uint DataSocket, int Mode, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "success";
            Occurred = false;
            int state = RseeController_P_S2_24V_10TCL_TriMode(DataSocket, Mode);
            if (state != 0)
            {
                StateOut(state, out ErrorCode, out ErrorText, out Occurred);
                ErrorText = "TriMode" + ErrorText;
                Occurred = true;
                return false;
            }
            return true;
        }

        /// <summary>
        /// 打开光源
        /// </summary>
        /// <param name="DataSocket"></param>
        /// <param name="LightData"></param>
        /// <param name="LightTime"></param>
        /// <param name="ErrorCode"></param>
        /// <param name="ErrorText"></param>
        /// <param name="Occurred"></param>
        /// <returns></returns>
        public override bool OpenLight(uint DataSocket, int[] LightData, int LightTime, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "success";
            Occurred = false;
            int state1 = RseeController_P_S2_24V_10TCL_SetRange(DataSocket, LightData);
            if (state1 != 0)
            {
                StateOut(state1, out ErrorCode, out ErrorText, out Occurred);
                ErrorText = "OpenLight_SetRange" + ErrorText;
                Occurred = true;
                return false;
            }
            int state2 = RseeController_P_S2_24V_10TCL_LimitTime(DataSocket, LightTime);
            if (state2 != 0)
            {
                StateOut(state2, out ErrorCode, out ErrorText, out Occurred);
                ErrorText = "OpenLight_LimitTime" + ErrorText;
                Occurred = true;
                return false;
            }
            int state3 = RseeController_P_S2_24V_10TCL_Onoff(DataSocket, true);
            if (state3 != 0)
            {
                StateOut(state3, out ErrorCode, out ErrorText, out Occurred);
                ErrorText = "OpenLight_DataSocket" + ErrorText;
                Occurred = true;
                return false;
            }
            return true;
        }

        /// <summary>
        /// 关闭光源
        /// </summary>
        /// <param name="DataSocket"></param>
        /// <param name="LightTime"></param>
        /// <param name="ErrorCode"></param>
        /// <param name="ErrorText"></param>
        /// <param name="Occurred"></param>
        /// <returns></returns>
        public override bool CloseLight(uint DataSocket, int LightTime, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "success";
            Occurred = false;

            int state = RseeController_P_S2_24V_10TCL_Onoff(DataSocket, false);
            if (state != 0)
            {
                StateOut(state, out ErrorCode, out ErrorText, out Occurred);
                ErrorText = "CloseLight" + ErrorText;
                Occurred = true;
                return false;
            }
            return true;
        }

        /// <summary>
        /// 在线/离线模式
        /// </summary>
        /// <param name="DataSocket"></param>
        /// <param name="offlineOrOnline"></param>
        /// <param name="ErrorCode"></param>
        /// <param name="ErrorText"></param>
        /// <param name="Occurred"></param>
        /// <returns></returns>
        public override bool OfflineOrOnline(uint DataSocket, bool offlineOrOnline, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = "success";
            Occurred = false;
            int state = RseeController_P_S2_24V_10TCL_TriEn(DataSocket, offlineOrOnline);
            if (state != 0)
            {
                StateOut(state, out ErrorCode, out ErrorText, out Occurred);
                ErrorText = "OfflineOrOnline" + ErrorText;
                Occurred = true;
                return false;
            }
            return true;
        }

        /// <summary>
        /// 错误信息输出
        /// </summary>
        /// <param name="ReturnValue"></param>
        /// <param name="ErrorCode"></param>
        /// <param name="ErrorText"></param>
        /// <param name="Occurred"></param>
        /// <returns></returns>
        public static bool StateOut(int ReturnValue, out int ErrorCode, out string ErrorText, out bool Occurred)
        {

            ErrorCode = 0;
            ErrorText = "success";
            Occurred = false;
            if (ReturnValue == -1)
            {
                ErrorCode = -1;
                ErrorText = "设置/读取失败";
                Occurred = false;

            }
            else if (ReturnValue == -2)
            {
                ErrorCode = -2;
                ErrorText = "指令格式出错";
                Occurred = false;
            }
            else if (ReturnValue == -3)
            {
                ErrorCode = -3;
                ErrorText = "校验出错";
                Occurred = false;
            }
            else if (ReturnValue == -4)
            {
                ErrorCode = -4;
                ErrorText = "指令代码编号错误";
                Occurred = false;
            }
            else if (ReturnValue == -5)
            {
                ErrorCode = -5;
                ErrorText = "指令参数不在范围内";
                Occurred = false;
            }
            else if (ReturnValue == -6)
            {
                ErrorCode = -6;
                ErrorText = "指令使用场合不正确";
                Occurred = false;
            }
            else if (ReturnValue == -7)
            {
                ErrorCode = -7;
                ErrorText = "存储区无数据 ";
                Occurred = false;
            }
            else if (ReturnValue == -8)
            {
                ErrorCode = -8;
                ErrorText = "设置步骤超出流程长度";
                Occurred = false;
            }
            return true;
        }
    }
}
